package com.huishu.coupon.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.exception.BaseException;
import com.huishu.coupon.mapper.CouponHistoryMapper;
import com.huishu.coupon.mapper.CouponMapper;
import com.huishu.coupon.pojo.model.CouponDO;
import com.huishu.coupon.pojo.model.CouponHistoryDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName：CouponScheduled
 * @Author: ljj
 * @Date: 2023/4/20 17:20
 */
@Slf4j
@Component
public class CouponScheduled {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponHistoryMapper couponHistoryMapper;

    /**
     * 查询
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void expireCoupon() {
        // 查询过期优惠卷
        LambdaQueryWrapper<CouponDO> queryWrapper = Wrappers.lambdaQuery(CouponDO.class)
                .eq(CouponDO::getIsExpire, 0)
                .le(CouponDO::getEndDate, new Date());
        List<CouponDO> coupons = couponMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(coupons)) {
            List<Long> couponIds = coupons.stream().map(CouponDO::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<CouponDO> uw = Wrappers.lambdaUpdate(CouponDO.class)
                    .set(CouponDO::getIsExpire, 1)
                    .set(CouponDO::getIsRelease, 0)
                    .in(CouponDO::getId, couponIds);
            int update = couponMapper.update(null, uw);
            if (update <= 0) {
                throw new BaseException("定时判断优惠卷过期异常！！");
            }
            LambdaUpdateWrapper<CouponHistoryDO> uw1 = Wrappers.lambdaUpdate(CouponHistoryDO.class)
                    .set(CouponHistoryDO::getUseStatus, 2)
                    .ne(CouponHistoryDO::getUseStatus, 1)
                    .in(CouponHistoryDO::getCouponId, couponIds);
            couponHistoryMapper.update(null, uw1);
        }

    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void expireUserCoupon() {
        // 查询过期优惠卷
        LambdaQueryWrapper<CouponHistoryDO> queryWrapper = Wrappers.lambdaQuery(CouponHistoryDO.class)
                .le(CouponHistoryDO::getOverdueDate, new Date());
        List<CouponHistoryDO> couponHistorys = couponHistoryMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(couponHistorys)) {
            List<Long> ids = couponHistorys.stream().map(CouponHistoryDO::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<CouponHistoryDO> uw1 = Wrappers.lambdaUpdate(CouponHistoryDO.class)
                    .set(CouponHistoryDO::getUseStatus, 2)
                    .ne(CouponHistoryDO::getUseStatus, 1)
                    .in(CouponHistoryDO::getCouponId, ids);
            couponHistoryMapper.update(null, uw1);
        }
    }
}
